#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
#define int long long
// #define DEBUG
const int N=1e5+10;

int n;

struct Fruit
{
    int w, t;
    bool operator<(const Fruit& f) const
    {
        return w>f.w;
    }
}fruit[N];
priority_queue<Fruit> q; //价值小根堆

bool cmp(Fruit a, Fruit b)
{
    return a.t<b.t;
}

signed main()
{
    #ifdef DEBUG
        freopen("../in.txt", "r", stdin);
        freopen("../out.txt", "w", stdout);
    #endif
    scanf("%d", &n);
    for(int i=1; i<=n; i++) scanf("%d %d", &fruit[i].w, &fruit[i].t);
    sort(fruit+1, fruit+1+n, cmp); //时间排序
    for(int i=1; i<=n; i++)
    {
        //购物车位置充足，直接放
        if(fruit[i].t>q.size()) //有足够的时间卖出
            q.push(fruit[i]);
        else //购物车已满，不能卖出，判断最低价值和当前价值，去大的
        { 
            Fruit top=q.top();
            if(fruit[i].w>top.w)
            {
                q.pop(); q.push(fruit[i]);
            }
        }
    }
    int sum=0;
    while(q.size())
    {
        auto u=q.top(); q.pop();
        sum+=u.w;
    }
    printf("%lld\n", sum);
    return 0;
}